package slh.favourite;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FavouriteDao {
    private Connection conn;

    public FavouriteDao(Connection conn) {
        this.conn = conn;
    }

    // 添加收藏
    public boolean addFavorite(String userId, int contentId, String contentType) throws SQLException {
        String sql = "INSERT INTO favorite (USERID, CONTENT_ID, CONTENT_TYPE) VALUES (?, ?, ?)";
        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, userId);
            stmt.setInt(2, contentId);
            stmt.setString(3, contentType);
            return stmt.executeUpdate() > 0;
        }
    }

    // 取消收藏
    public boolean removeFavorite(String userId, int contentId, String contentType) throws SQLException {
        String sql = "DELETE FROM favorite WHERE USERID = ? AND CONTENT_ID = ? AND CONTENT_TYPE = ?";
        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, userId);
            stmt.setInt(2, contentId);
            stmt.setString(3, contentType);
            return stmt.executeUpdate() > 0;
        }
    }

    // 检查是否已收藏
    public boolean isFavorite(String userId, int contentId, String contentType) throws SQLException {
        String sql = "SELECT COUNT(*) AS count FROM favorite WHERE USERID = ? AND CONTENT_ID = ? AND CONTENT_TYPE = ?";
        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, userId);
            stmt.setInt(2, contentId);
            stmt.setString(3, contentType);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt("count") > 0;
            }
            return false;
        }
    }

    // 获取用户收藏列表
    public List<Integer> getUserFavorites(String userId, String contentType) throws SQLException {
        List<Integer> favorites = new ArrayList<>();
        String sql = "SELECT CONTENT_ID FROM favorite WHERE USERID = ? AND CONTENT_TYPE = ? ORDER BY CREATED_AT DESC";
        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, userId);
            stmt.setString(2, contentType);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                favorites.add(rs.getInt("CONTENT_ID"));
            }
        }
        return favorites;
    }


    /***
     * 获取一个用户收藏的电影名、书名
     * @param userId
     * @return
     * @throws SQLException
     */
    public ArrayList<String> getUserFavoriteContent(String userId) throws SQLException {
        ArrayList<String> favorites = new ArrayList<>();
        ArrayList<Integer> movieContentIdList = (ArrayList<Integer>)getUserFavorites(userId, "movie");
        ArrayList<Integer> bookContentIdList = (ArrayList<Integer>)getUserFavorites(userId, "book");

        // 查询电影标题
        if (!movieContentIdList.isEmpty()) {
            String movieQuery = "SELECT TITLE FROM movie WHERE MOVIE_ID = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(movieQuery)) {
                for (Integer movieContentId : movieContentIdList) {
                    pstmt.setInt(1, movieContentId);
                    try (ResultSet rs = pstmt.executeQuery()) {
                        if (rs.next()) {
                            favorites.add("电影"+rs.getString("TITLE"));
                        }
                    }
                }
            }
        }

        // 查询书籍标题
        if (!bookContentIdList.isEmpty()) {
            String bookQuery = "SELECT TITLE FROM book WHERE BOOK_ID = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(bookQuery)) {
                for (Integer bookContentId : bookContentIdList) {
                    pstmt.setInt(1, bookContentId);
                    try (ResultSet rs = pstmt.executeQuery()) {
                        if (rs.next()) {
                            favorites.add("图书"+rs.getString("TITLE"));
                        }
                    }
                }
            }
        }
        return favorites;
    }
}